//
//  CustomUIImage.m
//  ELife
//
//  Created by feng lu on 11-6-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CustomUIImage.h"


@implementation UIImage (CustomUIImage)
- (UIImage*)scaleWithSize:(CGSize)size {
    CGFloat imageWidth = self.size.width;
    CGFloat imageHeight = self.size.height;
    
    CGFloat wScale = imageWidth / size.width;
    CGFloat hScale = imageHeight / size.height;
    NSLog(@"ws = %f, hs = %f", wScale, hScale);
    
    CGSize newSize = self.size;
    BOOL needRedraw;
    if (wScale > hScale && wScale > 1) {
        newSize.width = size.width;
        newSize.height = imageHeight / wScale;
        needRedraw = YES;
    }
    else if (hScale > wScale && hScale > 1) {
        newSize.width = imageWidth / hScale;
        newSize.height = size.height;
        needRedraw = YES;
    }
    
    NSLog(@"w = %f, h = %f", newSize.width, newSize.height);
    
    if (needRedraw) {
        UIGraphicsBeginImageContext(newSize);
        [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
        self = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return self;
}
@end
